package com.raincat.multimediapicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.KeyEvent;

import com.bumptech.glide.Glide;
import com.raincat.multimediapicker.utils.PickerConfig;

import java.util.ArrayList;
import java.util.List;

public class PickerActivity extends FragmentActivity {
    private Context context;

    private List<Fragment> fragments = new ArrayList<>();

    public FragmentTabAdapter fragmentTabAdapter;
    public PickerConfig pickerConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker_activity_main);

        context = PickerActivity.this;
        pickerConfig = (PickerConfig) getIntent().getSerializableExtra("Config");
        if (pickerConfig == null)
            finish();

        if (pickerConfig.mode < 2)
            fragments.add(new LatelyImageFragment());
        else
            fragments.add(new VideoGridFragment());

        fragmentTabAdapter = new FragmentTabAdapter(this, fragments, R.id.tab_content);
    }

    public float getScale() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.density;
    }

    public int getPhoneW() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    public void finishActivity(String resultKey, ArrayList<String> result) {
        Intent intent = new Intent();
        intent.putStringArrayListExtra(resultKey, result); // 将计算的值回传回去
        setResult(Activity.RESULT_OK, intent);
        finish();
        overridePendingTransition(0, R.anim.push_bottom_out);
    }

    @Override
    protected void onDestroy() {
        fragmentTabAdapter = null;
        pickerConfig = null;
        Glide.get(context).clearMemory();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (fragmentTabAdapter.removeFragment()) {
                finish();
                overridePendingTransition(0, R.anim.push_bottom_out);
            }
            return true;
        }
        return false;
    }
}
